package com.education.welco.controller;


import com.education.common.core.BaseController;
import com.education.common.pojo.ResourceVO;
import com.education.common.utils.*;
import com.education.utils.BaiduAipContentCensor;
import com.education.welco.mapper.SCatalogResourceSynMapper;
import com.education.welco.pojo.*;
import com.education.welco.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 学校课程资源 信息操作处理
 *
 * @author d
 * @date 2020-05-08
 */
@Api(tags = {"PC-学校课程资源"})
@RestController
@RequestMapping("/sResources")
public class SResourcesController extends BaseController {

    @Autowired
    private ISResourcesService resourcesService;
    @Autowired
    private SCatalogResourceSynMapper resourceSynMapper;
    @Autowired
    private ISCatalogMenuService catalogMenuService;
    @Autowired
    private ISCatalogClassService catalogClassService;
    @Autowired
    private ITClassService classService;
    @Autowired
    private ISCatalogService catalogService;
    @Autowired
    private ISResourceClassService resourceClassService;
    @Autowired
    private ITSchooldeptService schooldeptService;
    @Autowired
    private BaiduAipContentCensor baiduAipContentCensor;


    /**
     * 查询资源
     *
     * @param sResources
     * @return
     */
    @GetMapping("/selectList")
    public ApiReturnObject selectList(SResources sResources, Integer pageSize, Integer pageNum) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<SResources> list = resourcesService.selectSResourcesList(sResources);
        return new ApiReturnObject("200", "success", new PageInfo<SResources>(list));
    }


    /**
     * feign : 备课资源同步
     */
    @PostMapping("/lessonResource")
    public void lessonResource(@RequestBody ResourceVO resourceVO) {
        SResources sResources = new SResources();
        sResources.setRUrl(resourceVO.getRUrl());
        sResources.setRName(resourceVO.getRName());
        sResources.setRSize(resourceVO.getRSize());
        sResources.setRCatalogid(resourceVO.getCatalogId());
        sResources.setType(resourceVO.getType());
        sResources.setResIco(resourceVO.getImage());
        sResources.setCreateTime(new Date());
        resourcesService.insertSResources(sResources);
    }


    /**
     * fengn : 虚拟教学同步--资源课程体系
     */
    @PostMapping("/insertResourcesVirtual")
    public void insertResourcesVirtual(@RequestBody ResourceVO resourceVO) {
        //添加虚拟教学
        SResources sResources = new SResources();
        sResources.setRName(resourceVO.getRName());
        sResources.setRUrl(resourceVO.getRUrl());
        sResources.setRCatalogid(resourceVO.getCatalogId());
        sResources.setType("8");
        sResources.setCreateTime(new Date());
        resourcesService.insertSResources(sResources);
        //切割分别添加
        String[] split = resourceVO.getRUrl().split(",");
        //3D
        if (!"0".equals(split[0])) {
            SResources threeD = new SResources();
            threeD.setRName(resourceVO.getRName());
            threeD.setRUrl(split[0]);
            threeD.setRCatalogid(resourceVO.getCatalogId());
            threeD.setType("6");
            threeD.setCreateTime(new Date());
            resourcesService.insertSResources(threeD);
        }
        //flash
        if (!"0".equals(split[1])) {
            SResources flash = new SResources();
            flash.setRName(resourceVO.getRName());
            flash.setRUrl(split[1]);
            flash.setRCatalogid(resourceVO.getCatalogId());
            flash.setType("7");
            flash.setCreateTime(new Date());
            resourcesService.insertSResources(flash);
        }

    }


    /**
     * feign : 远程调用同步资源--资源课程体系
     *
     * @return
     */
    @PostMapping("/insertResources")
    public String insertResources(@RequestBody ResourceVO resourceVO) {
        SResources sResources = new SResources();
        BeanUtils.copyProperties(resourceVO, sResources);
        sResources.setRCatalogid(resourceVO.getCatalogId());
        sResources.setCreateTime(new Date());
        //二期资源课程体系的类型 : 1 TXT 2 PPT 3 Word 4 小视频 5 图片 6 PDF 7 其他 8 3D动画 9 FLASH 动画
        //学校课程的类型 : 1 教学课件  2 技术文档  3 教学笔记 4 图片 5 真人实操视频  6 3D 7 flash 8虚拟教学 9录播视频
        switch (resourceVO.getType()) {
            case "1":
                sResources.setType("1");
                break;
            case "3":
                sResources.setType("2");
                break;
            case "4":
                sResources.setType("5");
                break;
            case "5":
                sResources.setType("4");
                sResources.setResIco(resourceVO.getRUrl());
                break;
        }
        int i = resourcesService.insertSResources(sResources);
        if (i > 0) {
            return "1";
        } else {
            return "0";
        }
    }


//    @ApiOperation(value = "二期--查询课程下的资源", httpMethod = "GET")
//    @GetMapping("/selectResourcesByType")
//    public ApiReturnObject selectResourcesByType(String type, Integer catalogId) {
//        SResources sResources = new SResources();
//        sResources.setType(type);
//        sResources.setRCatalogid(catalogId);
//        List<SResources> resourcesList = resourcesService.selectSResourcesList(sResources);
//        return new ApiReturnObject("200", "success", resourcesList.size(), resourcesList);
//    }
//
//
//    @ApiOperation(value = "二期同步资源--免费公开课", httpMethod = "POST")
//    @PostMapping("/insertFreeClass")
//    public ApiReturnObject insertFreeClass(Integer id, Integer catalogId) {
//        Map<String, Object> map = onlineFeign.selectFreeClassById(id);
//        //同步
//        SResources sResources = new SResources();
//        sResources.setResIco(map.get("image").toString());
//        sResources.setRName(map.get("name").toString());
//        sResources.setRUrl(map.get("url").toString());
//        sResources.setType("5");//同步到实操视频
//        sResources.setRCatalogid(catalogId);//课程名称
//        sResources.setCreateTime(new Date());
//
//        resourcesService.insertSResources(sResources);
//
//        return new ApiReturnObject("200", "success", "success");
//    }

//    @ApiOperation(value = "二期同步资源--专业课程体系", httpMethod = "POST")
//    @PostMapping("/insertResources")
//    public ApiReturnObject insertResources(Integer[] ids, Integer type, Integer catalogId) {
//        List<OResources> oResourcesList = onlineFeign.selectResources(ids, type);
//
//        for (OResources sResources : oResourcesList) {
//            //如果是虚拟教学  则切割开存储到 6 : 3D 7 : flash 里
//            if (sResources.getType().equals("8")) {
//                String[] split = sResources.getRUrl().split(",");
//                // 3d
//                SResources threeD = new SResources();
//                threeD.setRUrl(split[0]);
//                threeD.setRName(sResources.getRName());
//                threeD.setRCatalogid(catalogId);
//                threeD.setCreateTime(new Date());
//                threeD.setType("6");
//                resourcesService.insertSResources(threeD);
//                //flash
//                SResources flash = new SResources();
//                flash.setRUrl(split[1]);
//                flash.setRName(sResources.getRName());//名称
//                flash.setRCatalogid(catalogId);//课程id
//                flash.setCreateTime(new Date());
//                flash.setType("7");//类型
//                resourcesService.insertSResources(flash);
//            }
//            //存储资源
//            sResources.setCreateTime(new Date());
//            SResources resources = new SResources();
//            resources.setRUrl(sResources.getRUrl());
//            resources.setRName(sResources.getRName());
//            resources.setRCatalogid(catalogId);
//            resources.setCreateTime(new Date());
//            resources.setType(sResources.getType());
//            resources.setResIco(sResources.getImage());
//            resourcesService.insertSResources(resources);
//        }
//        return new ApiReturnObject("200", "success", "success");
//    }

    /**
     * 获取到最上面一级的id     查看班级列表 课程分享班级
     */
    public Integer GetFirstId2(Integer catalogId) {
        SCatalogMenu sCatalogMenu = catalogMenuService.selectSCatalogMenuById(catalogId);
        //第二级
        if (sCatalogMenu.getParentId() != 0) {
            //第一级
            SCatalogMenu sCatalogMenu1 = catalogMenuService.selectSCatalogMenuById(sCatalogMenu.getParentId());
            return GetFirstId2(sCatalogMenu1.getId());
        }
        return sCatalogMenu.getId();
    }


    /**
     * 查询资源查看班级
     */
    @GetMapping("/resClassList")
    public ApiReturnObject resourceClass(Integer resourceId) {
        if (resourceId != null) {
            List<TClass> classes = classService.selectTClassByResourceId(resourceId);
            if (classes.size() > 0) {
                for (TClass tClass : classes) {
//                    TSchooldept schooldept = schooldeptService.selectTSchooldeptByClassId(tClass.getId());
                    TSchooldept schooldept = schooldeptService.selectTSchooldeptByClassId(tClass.getDeptId());
                    tClass.setDeptId(schooldept.getId());
                    tClass.setDeptName(schooldept.getDeptName());
                }
                return new ApiReturnObject("200", "查询成功", classes);
            }
            return new ApiReturnObject("200", "结果为空", classes);
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }


    /**
     * 查询资源查看内容
     */
    @GetMapping("/resContentList")
    public ApiReturnObject resourceContent(Integer catalogId, String type) {
        if (catalogId != null && type != null && !"".equals(type)) {
            SResources sResources = new SResources();
            sResources.setCatalogId(catalogId);
            sResources.setType(type);
            //资源列表
            resourcesService.selectSCatalogMenuByCatalogId(sResources);
            List<TClass> classes = classService.selectTClassByResourceId(catalogId);

            return new ApiReturnObject("200", "查询成功", classes);
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }


    /**
     * 查询资源查看班级
     */
    public List<TClass> resourceClass2(Integer resourceId) {
        List<TClass> tClasses = classService.selectTClassByResourceId(resourceId);
        if (tClasses.size() > 0) {
            for (TClass tClass : tClasses) {
//                TSchooldept schooldept = schooldeptService.selectTSchooldeptByClassId(tClass.getId());
                TSchooldept tSchooldept = schooldeptService.selectTSchooldeptByClassId(tClass.getDeptId());
                tClass.setDeptId(tSchooldept.getId());
                tClass.setDeptName(tSchooldept.getDeptName());
            }
        }
        return tClasses;
    }


    /**
     * 根据课程小节的id   查询出资源  只返回一类型
     */
    @Transactional
    @GetMapping("/list")
    public ApiReturnObject list(Integer catalogId, String type, Integer pageSize, Integer pageNum) {
        //根据当前登录老师id 查询出列表
        if (catalogId != null && type != null && !"".equals(type)) {
            SResources resources = new SResources();
            resources.setRCatalogid(catalogId);
//            if(type != null && !"".equals(type)){
            //资源的分类     资源类型  1 教学课件  2 技术文档  3 教学笔记 4 图片 5 视频  6 3D 7 flash   根据类型查询出对应的资源
            resources.setType(type);
            //同步上课资料
            resources.setSynch("1");
            List<SResources> sResourceList = resourcesService.selectSResourcesList(resources);
            if (sResourceList != null && sResourceList.size() > 0) {
                //格式化创建时间
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                for (SResources sResource : sResourceList) {
                    //该资源查看班级
                    List<TClass> sResourceClasses = resourceClass2(resources.getId());
                    if (sResourceClasses != null && sResourceClasses.size() > 0) {
                        sResource.setClassList(sResourceClasses);
                    }
                    String format = simpleDateFormat.format(sResource.getCreateTime());
                    sResource.setCreateTimeString(format);

                    if ("8".equals(sResource.getType())) {
                        if (sResource.getRUrl() != null && !"".equals(sResource.getRUrl())) {
                            if (sResource.getRUrl().contains(",")) {
                                //true 切割
                                String[] split = sResource.getRUrl().split(",");
                                if (split.length > 1) {
                                    //两个路径都有
                                    //上传文件map 3d集合
                                    sResource.setThreeUrl(split[0]);

                                    //上传文件map flash集合
                                    sResource.setFlashUrl(split[1]);

                                } else if (split.length == 1) {
                                    // 3d
                                    //上传文件map 3d集合
                                    sResource.setThreeUrl(split[0]);
                                }
                            } else {
                                //flash
                                sResource.setFlashUrl(sResource.getRUrl());
                            }
                        }
                    }
                }
            }
            return new ApiReturnObject("200", "查询成功", sResourceList);
//            }
        }
        return new ApiReturnObject("201", "缺少相关参数", "");
    }


    /**
     * 列表查询
     * 根据二级目录id，以及资源类型type查找对应的资源列表
     *
     * @param catalogId 二级目录id
     * @param type      资源类型
     * @param rName     资源名称
     * @param pageSize  分页页码
     * @param pageNum   分页数量
     * @return 结果
     * select id, r_number, r_name, r_url, r_catalogId, r_browsing, r_size, create_time, del_flag, r_state, r_remark, r_createUser,
     * r_uploadTime, type, is_stu, synch, res_ico from s_resources WHERE r_catalogId = 753 and type = '1'
     */
    @Transactional
    @GetMapping("/listOther")
    public ApiReturnObject listOther(Integer catalogId, String type, String rName, Integer pageSize, Integer pageNum) {
        //判断参数是否完整
        if (catalogId != null && type != null && !"".equals(type)) {
            //构建对象传入参数
            SResources resources = new SResources();
            resources.setRCatalogid(catalogId);
            resources.setRName(rName);
            if (type != null && !"".equals(type)) {
                //资源的分类    资源类型  1 教学课件  2 技术文档  3 教学笔记 4 图片 5 视频  6 3D 7 flash   根据类型查询出对应的资源
                resources.setType(type);
                //查询服务调用
                List<SResources> sResourceList = resourcesService.selectSResourcesList(resources);
                //处理查询结果
                if (sResourceList != null && sResourceList.size() > 0) {
                    //格式化创建时间
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    //list遍历
                    for (SResources sResource : sResourceList) {
                        //对资源名称做操作
                        if (sResource.getRUrl() != null && !"".equals(sResource.getRUrl())) {
                            //后缀suffix
                            String[] split = sResource.getRUrl().split("\\.");
                            String suffix = split[split.length-1];
                            if (suffix.contains(",")) {
                                suffix = suffix.split(",")[0];
                            }
                            //名字拼接文件后缀名
                            sResource.setRName(sResource.getRName() + "." + suffix);
                        }
                        //格式化时间
                        String format = simpleDateFormat.format(sResource.getCreateTime());
                        sResource.setCreateTimeString(format);
                    }
                }
                return new ApiReturnObject("200", "查询成功", sResourceList);
            }
        }
        return new ApiReturnObject("201", "缺少相关参数", "");
    }

    /**
     * 修改此门备课的状态
     */
    public SCatalogMenu changeStatus(SResources resources) {
        //将资源所在的备课状态改变
        Integer rCatalogId = resources.getRCatalogid();
        //教师创建课程表
        SCatalogMenu catalogMenu = catalogMenuService.selectSCatalogMenuById(rCatalogId);
        //目录最下面一级
        Integer parentId = catalogMenu.getParentId();
        //备课课程id
        Integer integer = GetFirstId2(parentId);
        SCatalogMenu catalogMenu1 = catalogMenuService.selectSCatalogMenuById(integer);
        return catalogMenu1;
    }

    /**
     * 根据资源id主键查找-详情查找
     *
     * @param id 资源id主键
     * @return 结果
     * select id, r_number, r_name, r_url, r_catalogId, r_browsing, r_size, create_time, del_flag, r_state, r_remark,
     * r_createUser, r_uploadTime, type, is_stu, synch, res_ico from s_resources where id = 3935
     */
    @GetMapping("/selectOne")
    public ApiReturnObject selectOne(Integer id) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("informatization4.0/1592560665193.jpg");
        strings.add("informatization4.0/1592560665464.jpg");
        strings.add("informatization4.0/1592560665331.jpg");
        strings.add("informatization4.0/1592560665621.jpg");
            strings.add("informatization4.0/1592560665719.jpg");
        //主键查找服务
        SResources sResource = resourcesService.selectSResourcesById(id);
        //判断图片的类型
        if (strings.contains(sResource.getResIco())) {
            sResource.setIcoType("1");
        } else {
            sResource.setIcoType("2");
        }
        //暂定
        /*if ("8".equals(sResource.getType())) {
            if (sResource.getRUrl() != null && !"".equals(sResource.getRUrl())) {
                if (sResource.getRUrl().contains(",")) {
                    //true 切割
                    String[] split = sResource.getRUrl().split(",");
                    if (split.length > 1) {
                        //两个路径都有
                        //上传文件map 3d集合
                        HashMap<String, Object> map = new HashMap<>();
                        if (sResource.getRName() != null) {
                            map.put("name", sResource.getRName());
                        }
                        map.put("url", split[0]);
                        sResource.setFileList(map);

                        //上传文件map flash集合
                        HashMap<String, Object> map2 = new HashMap<>();
                        if (sResource.getRName() != null) {
                            map2.put("name", sResource.getRName());
                        }
                        map2.put("url", split[1]);
                        sResource.setFileList2(map2);
                    } else if (split.length == 1) {
                        // 3d
                        //上传文件map 3d集合
                        HashMap<String, Object> map = new HashMap<>();
                        if (sResource.getRName() != null) {
                            map.put("name", sResource.getRName());
                        }
                        map.put("url", split[0]);
                        sResource.setFileList(map);
                    }
                } else {
                    //flash
                    HashMap<String, Object> map2 = new HashMap<>();
                    if (sResource.getRName() != null) {
                        map2.put("name", sResource.getRName());
                    }
                    if (sResource.getRUrl() != null) {
                        map2.put("url", sResource.getRUrl());
                    }
                    sResource.setFileList2(map2);
                }
            }
        } else {
            //上传文件map 集合
            HashMap<String, Object> map = new HashMap<>();
            if (sResource.getRName() != null) {
                map.put("name", sResource.getRName());
            }
            if (sResource.getRUrl() != null) {
                map.put("url", sResource.getRUrl());
            }
            sResource.setFileList(map);
        }*/

        //文件内容
        Map<String, Object> map = new HashMap<>();
        if (sResource.getRName() != null) {
            map.put("name", sResource.getRName());
        }
        if (sResource.getRUrl() != null) {
            map.put("url", sResource.getRUrl());
        }
        sResource.setFileList(map);

        //是否同步
        /*if (sResource.getSynch() != null) {
            if ("0".equals(sResource.getSynch())) {
                sResource.setSynchBool(false);
            } else if ("1".equals(sResource.getSynch())) {
                sResource.setSynchBool(true);
            }
        }*/
        //是否允许学生查看
        /*if (sResource.getIsStu() != null) {
            if ("0".equals(sResource.getIsStu())) {
                sResource.setStuBool(false);
            } else if ("1".equals(sResource.getIsStu())) {
                sResource.setStuBool(true);
            }
        }*/

        //该资源查看班级
        /*List<TClass> sResourceClasses = resourceClass2(sResource.getId());
        if (sResourceClasses != null && sResourceClasses.size() > 0) {
            sResource.setClassList(sResourceClasses);
            String s = "";
            int size = sResourceClasses.size();
            //拼接查看班级字符串
            for (int i = 0; i < size; i++) {
                if (i == size - 1) {
                    //最后一个
                    s = s + sResourceClasses.get(i).getClassName();
                } else {
                    s = s + sResourceClasses.get(i).getClassName() + ",";
                }
            }
            sResource.setCheckString(s);
        }*/

        return new ApiReturnObject("200", "查询成功", sResource);
    }


    public SResources updateStatus(SResources resources) {
        //app 端查看 允许
        String i = "1";
        if (i.equals(resources.getIsStu())) {
            //
            SCatalogMenu catalogMenu = changeStatus(resources);
            if (!i.equals(catalogMenu.getIsStu())) {
                //修改备课是否允许学生查看状态
                catalogMenu.setIsStu("1");
                catalogMenuService.updateSCatalogMenu(catalogMenu);
            }
        }
        // 同步到上课资料
        if (i.equals(resources.getSynch())) {
            //同步上课资料
            SCatalogMenu catalogMenu = changeStatus(resources);
            if (!i.equals(catalogMenu.getIsSyn())) {
                //修改同步上课资料状态
                catalogMenu.setIsSyn("1");
                catalogMenuService.updateSCatalogMenu(catalogMenu);
            }
        }
        return resources;
    }


    public void addZhongJian(SResources resources) {
        Integer id = resources.getId();
        //添加资源查看班级中间表数据
        if (resources.getClassList() != null && resources.getClassList().size() > 0) {
            SResourceClass sResourceClass = new SResourceClass();
            sResourceClass.setResourceId(id);
            sResourceClass.setCreateTime(new Date());
            for (TClass tClass : resources.getClassList()) {
                sResourceClass.setClassId(tClass.getId());
                resourceClassService.insertSResourceClass(sResourceClass);
            }
        }
    }


    /**
     * 老师备课模块添加资源
     * 逻辑删除-添加中间表中允许查看的班级
     *
     * @param resources 参数罗列详细
     *                  rName: "郭教学课件2"   资源名称
     *                  rUrl: "1661412662330.doc"  七牛路径
     *                  topCatalogId: 父级目录id
     *                  catalogId: 753  二级目录id
     *                  rCreateuser: 创建人id
     *                  type: 1   资源类型
     *                  resIco: "informatization4.0/1592560665193.jpg"   封面
     *                  <p>
     *                  classList: [] ❌
     *                  isStu: ""   ❌
     *                  synch: ""   ❌
     *                  <p>
     *                  补充：topCatalogId课程id(0级)
     *                  rNumber、rName、rUrl、topCatalogId、rCatalogid、rSize、createTime、rCreateuser、rUploadtime、type、resIco
     * @return 4 实战图片  5 实景视频  7 FLASH微课
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody SResources resources) {
        //资源序列号
        resources.setRNumber(UUIDUtils.getByUUId());//机器号+时间+随机数
        //传的转到表字段
        resources.setRCatalogid(resources.getCatalogId());
        //获取文件大小
        resources.setRSize(FileGetSizeUtil.getQiNiuUrl2(resources.getRUrl()));
        //设置时间
        resources.setCreateTime(new Date());
        resources.setRUploadtime(DateUtils.dateTime(new Date()));
        List<String> resTypeList = new ArrayList<String>() {{
            add("4");
            add("5");
            add("7");
        }};
        if (resTypeList.contains(resources.getType())) {
            resources.setRState("0");
        }
        //针对图片类型 即封面
        if ("4".equals(resources.getType())) {
            resources.setResIco(resources.getRUrl());
            String sname = resources.getRName().split("\\.")[0];
            if (sname != null) {
                resources.setRName(sname);
            }
        }
        resourcesService.insertSResources(resources);
        if (resTypeList.contains(resources.getType()) && FileGetSizeUtil.getQiNiuUrlSize(resources.getRUrl()) < 52428800) {
            baiduAipContentCensor.singleFileAddSave(resources.getRUrl(), "s_resources",
                    "4".equals(resources.getType()) ? "1" : "2");
        } else if (resTypeList.contains(resources.getType()) && FileGetSizeUtil.getQiNiuUrlSize(resources.getRUrl()) >= 52428800) {
            SResources resources1 = new SResources();
            resources1.setId(resources.getId());
            resources1.setRState("1");
            resourcesService.updateSResources(resources1);
        }
        return new ApiReturnObject("200", "添加成功", "");
    }


    /**
     * 分享资源
     */
    @PostMapping("/share")
    public ApiReturnObject share(@RequestBody SResources resources) {
        if (resources.getResourceIdList() != null && resources.getResourceIdList().size() > 0) {
            if (resources.getRCatalogid() != null) {
                List<SResources> sResources = resourcesService.selectSResourcesByIds(resources.getResourceIdList());
                SCatalogResourceSyn resourceSyn = new SCatalogResourceSyn();
                resourceSyn.setCatalogId(resources.getRCatalogid());
                resourceSyn.setResourceType(resourceSyn.getResourceType());
                for (SResources sResource : sResources) {
                    resourceSyn.setResourceId(sResource.getId());
                    //往中间表中添加数据
                    resourceSynMapper.insertSCatalogResourceSyn(resourceSyn);
                    //设置资源的状态   app查看
                    if ("1".equals(resources.getIsStu())) {
                        sResource.setIsStu("1");
                    }
                    //同步上课资料
                    if ("1".equals(resources.getSynch())) {
                        sResource.setSynch("1");
                    }
                    resourcesService.updateSResources(sResource);
                }
                return new ApiReturnObject("200", "分享成功", "");
            }
            return new ApiReturnObject("201", "缺少catalogId参数", "");
        }
        return new ApiReturnObject("201", "请选择分享的内容", "");
    }


    /**
     * 主键修改课程
     *
     * @param resources id: 3935
     *                  isStu: ""
     *                  rName: "郭教学课件1"
     *                  resIco: "informatization4.0/1592560665193.jpg"
     *                  synch: "0"
     * @return
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody SResources resources) {
        //主键id判断
        if (resources.getId() != null) {
            //设置资源名称
            if (resources.getRName() != null && !"".equals(resources.getRName())) {
                String sname = resources.getRName().split("\\.")[0];
                if (sname != null) {
                    resources.setRName(sname);
                }
            }
            //资源班级关联逻辑删除
            /*if (resources.getClassList() != null && resources.getClassList().size() == 0) {
                //清空中间表数据
                resourceClassService.deleteSResourceClassByresourceId(resources.getId());
            }*/
            //再次进行资源班级关联逻辑删除
            /*if (resources.getClassList() != null && resources.getClassList().size() > 0) {
                //修改该资源的查看班级  先删除 再修改
                resourceClassService.deleteSResourceClassByresourceId(resources.getId());
                SResourceClass resourceClass = new SResourceClass();
                resourceClass.setResourceId(resources.getId());
                resourceClass.setCreateTime(new Date());
                for (TClass tClass : resources.getClassList()) {
                    resourceClass.setClassId(tClass.getId());
                    resourceClassService.insertSResourceClass(resourceClass);
                }
            }*/
            //判断修改数据的类型
            //SResources resources1 = resourcesService.selectSResourcesById(resources.getId());

            //3D和flash的逻辑(升级前)
            /*//3d flash 全都有
            if (resources.getRUrl() != null && !"".equals(resources.getRUrl()) && "8".equals(resources1.getType())) {
                if (resources.getRUrl().contains(",")) {
                    //true 切割
                    String[] split = resources.getRUrl().split(",");
                    if (split.length > 1) {
                        //两个路径都有
                        //修改3d  视频资源路径
                        if (resources.getThreeUrl() != null && !"".equals(resources.getThreeUrl())) {
                            //新的3d路径 + 原来的flash路径
                            resources.setRUrl(resources.getThreeUrl() + "," + split[1]);
                            //获取到3d路径
                            split = resources.getRUrl().split(",");
                        }
                        //修改 flash
                        if (resources.getFlashUrl() != null && !"".equals(resources.getFlashUrl())) {
                            //原来的3d路径 + 新的flash路径
                            resources.setRUrl(split[0] + resources.getFlashUrl());
                        }
                    } else if (split.length == 1) {
                        //修改3d      原路径只有3d
                        if (resources.getThreeUrl() != null && !"".equals(resources.getThreeUrl())) {
                            //新的3d路径 + 原来的flash路径
                            resources.setRUrl(resources.getThreeUrl() + ",");
                        }
                        //修改 flash
                        if (resources.getFlashUrl() != null && !"".equals(resources.getFlashUrl())) {
                            //原来的3d路径 + 新的flash路径
                            resources.setRUrl(resources.getRUrl() + resources.getFlashUrl());

                            //添加7的数据
                            SResources resources2 = new SResources();
                            //flash路径
                            resources2.setRUrl(resources.getFlashUrl());
                            resources2.setType("7");
                            //获取文件大小
                            String qiNiuUrl2 = FileGetSizeUtil.getQiNiuUrl2(resources.getFlashUrl());
                            resources2.setRSize(qiNiuUrl2);
                            if (resources2.getCatalogId() != null) {
                                resources2.setRCatalogid(resources.getCatalogId());
                            }
                            resources2.setRNumber(resources.getId().toString());
                            //编辑状态
                            resources2.setCreateTime(new Date());
                            resourcesService.insertSResources(resources2);

                        }
                    }
                } else {
                    String split = "";
                    //修改3d      原路径只有falsh
                    if (resources.getThreeUrl() != null && !"".equals(resources.getThreeUrl())) {
                        //新的3d路径 + 原来的flash路径
                        resources.setRUrl(resources.getThreeUrl() + "," + resources.getRUrl());
                        //获取到3d路径
                        split = resources.getRUrl().split(",")[0];

                        //添加6的数据
                        //添加7的数据
                        SResources resources2 = new SResources();
                        //flash路径
                        resources2.setRUrl(resources.getFlashUrl());
                        resources2.setType("6");
                        //获取文件大小
                        String qiNiuUrl2 = FileGetSizeUtil.getQiNiuUrl2(resources.getFlashUrl());
                        resources2.setRSize(qiNiuUrl2);
                        if (resources2.getCatalogId() != null) {
                            resources2.setRCatalogid(resources.getCatalogId());
                        }
                        resources2.setRNumber(resources.getId().toString());
                        //编辑状态
                        resources2.setCreateTime(new Date());
                        resourcesService.insertSResources(resources2);
                    }
                    //修改 flash
                    if (resources.getFlashUrl() != null && !"".equals(resources.getFlashUrl())) {
                        //原来的3d路径 + 新的flash路径
                        resources.setRUrl(split + resources.getFlashUrl());
                    }
                }
            } else {
                //原url为空
                //修改3d      原路径只有falsh
                if (resources.getThreeUrl() != null && !"".equals(resources.getThreeUrl())) {
                    //新的3d路径 + 原来的flash路径
                    resources.setRUrl(resources.getThreeUrl() + ",");

                    //添加6的数据
                    SResources resources2 = new SResources();
                    //flash路径
                    resources2.setRUrl(resources.getFlashUrl());
                    resources2.setType("6");
                    //获取文件大小
                    String qiNiuUrl2 = FileGetSizeUtil.getQiNiuUrl2(resources.getFlashUrl());
                    resources2.setRSize(qiNiuUrl2);
                    if (resources2.getCatalogId() != null) {
                        resources2.setRCatalogid(resources.getCatalogId());
                    }
                    resources2.setRNumber(resources.getId().toString());
                    //编辑状态
                    resources2.setCreateTime(new Date());
                    resourcesService.insertSResources(resources2);
                }
                //修改 flash
                if (resources.getFlashUrl() != null && !"".equals(resources.getFlashUrl())) {
                    //原来的3d路径 + 新的flash路径
                    resources.setRUrl(resources.getRUrl() + resources.getFlashUrl());

                    //添加7的数据
                    SResources resources2 = new SResources();
                    //flash路径
                    resources2.setRUrl(resources.getFlashUrl());
                    resources2.setType("7");
                    //获取文件大小
                    String qiNiuUrl2 = FileGetSizeUtil.getQiNiuUrl2(resources.getFlashUrl());
                    resources2.setRSize(qiNiuUrl2);
                    if (resources2.getCatalogId() != null) {
                        resources2.setRCatalogid(resources.getCatalogId());
                    }
                    resources2.setRNumber(resources.getId().toString());
                    //编辑状态
                    resources2.setCreateTime(new Date());
                    resourcesService.insertSResources(resources2);

                }
            }*/

            resources.setRRemark("资源已进行修改!");
            resourcesService.updateSResources(resources);
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少参数id", "");
    }


    /**
     * 资源主键删除  教学课件 flash微课 实景视频 课前导学
     *
     * @param id 资源主键id
     * @return
     */
    @GetMapping("/del")
    @Transactional
    public ApiReturnObject remove(Integer id) {
        if (id != null) {
//  TODO 教师上传资源一级平台导入资源 删除逻辑优化
            /*SResources resources = resourcesService.selectSResourcesById(id);
            if (StringUtils.isNotBlank(resources.getRUrl())) {
                try {
                    QiniuUtil.deleteFileFromQiniu(resources.getRUrl());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }*/
            resourcesService.deleteSResourcesById(id);
            return new ApiReturnObject("200", "删除成功", "");
        }
        return new ApiReturnObject("201", "缺少相关参数", "");
    }


    /**
     * ======================================================================为订单添加数据==============================================================================
     */
    //添加智慧云平台资源
    @GetMapping("/addRes")
    public String addRes() {
        //准备资源实例对象
        SResources resources = new SResources();
        //遍历文件夹下所有单个文件进行操作转移
        File file = new File("H:\\智慧云11.05新版资源\\3.11.车身电气虚拟软件\\3.真人实操视频\\");
        //获取文件夹下所有文件列表
        File[] files = file.listFiles();
        if (files == null) {
            System.out.println("failed");
            return "失败";
        }
        //遍历 文件列表   将文件属性赋值到 --->  资源实例对象
        for (File file1 : files) {
            resources.setRNumber(UUIDUtils.getByUUId());
            resources.setRCatalogid(674);
            resources.setResIco("informatization4.0/1592560665193.jpg");
            //教学课件 1    flash微课 7   实操视频 5
            resources.setType("5");
            resources.setSynch("0");
            resources.setIsStu("0");
            resources.setCreateTime(new Date());
            if (file1.getName().contains(".")) {
                String[] split = file1.getName().split("\\.");
                resources.setRName(split[1]);
            } else {
                resources.setRName(file1.getName());
            }
            resources.setRBrowsing(10);
            resources.setRSize("10MB");
            resources.setDelFlag("0");
            resources.setRState("0");
            resources.setRRemark(file1.getName());
            resources.setCreateUser("张教师");
            resources.setRUploadtime(DateUtils.getDate());

            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            long l = System.currentTimeMillis();
            resources.setRUrl(l + ".mp4");
            //重命名 移至另一个文件夹内
            boolean b = file1.renameTo(new File("H:\\智慧云11.05新版资源\\3.11.车身电气虚拟软件\\3.真人实操视频\\new\\" + l + ".mp4"));
            if (b) {
                resourcesService.insertSResources(resources);
            }
        }
        return "完成";
    }


    /**
     * 远程调用
     */
    @GetMapping("/resourceVOList")
    public List<ResourceVO> resourceVOList(Integer catalogId) {
        List<SCatalogMenu> sCatalogs = catalogMenuService.selectSCatalogMenuByParentId(catalogId);
        ArrayList<SResources> sResources = new ArrayList<>();
        for (SCatalogMenu sCatalog : sCatalogs) {
            List<SResources> resources = resourcesService.selectSResourcesByCatalogId(sCatalog.getId());
            sResources.addAll(resources);
        }
        List<ResourceVO> list = new ArrayList<>();
        for (SResources sResource : sResources) {
            ResourceVO resourceVO = new ResourceVO();
            BeanUtils.copyProperties(sResource, resourceVO);
            resourceVO.setCatalogId(null);
            list.add(resourceVO);
        }
        return list;
    }


    public static void main(String[] args) {
        String datetime = DateUtils.dateTimeNow();
        StringBuilder builder = new StringBuilder();
        String s = builder.append(datetime).append(UUIDUtils.getByUUId()).toString();
        System.out.println(s);
    }

}